/**
 * 产业带模块 API 接口
 * 包含标签管理、内容管理、会议管理等功能
 */

// =====================================================================================
// 类型定义 (Type Definitions)
// =====================================================================================

/**
 * 标签相关类型
 */
export interface Tag {
    id: number;
    tagName: string;
    createTime?: string;
    updateTime?: string;
}

export interface CreateTagRequest {
    tagName: string;
}

export interface TagListResponse {
    list: Tag[];
    total: number;
    pageNum: number;
    pageSize: number;
}

/**
 * 内容相关类型
 */
export type ContentType = 'VIDEO' | 'NEWS' | 'ARTICLE';

export interface Content {
    id: number;
    contentType: ContentType;
    title: string;
    coverImageUrl: string;
    summary: string;
    content: string;
    videoUrl?: string;
    durationMinutes?: number;
    publishDate: string;
    tags: Tag[];
    createTime?: string;
    updateTime?: string;
}

export interface CreateContentRequest {
    contentType: ContentType;
    title: string;
    coverImageUrl: string;
    summary: string;
    content: string;
    videoUrl?: string;
    durationMinutes?: number;
    publishDate: string;
    tagIds: number[];
}

export interface UpdateContentRequest extends CreateContentRequest {
    id: number;
}

export interface ContentListRequest {
    contentType?: ContentType;
    keyword?: string;
    pageNum?: number;
    pageSize?: number;
}

export interface ContentListResponse {
    list: Content[];
    total: number;
    pageNum: number;
    pageSize: number;
}

/**
 * 新接口：会议视频与会议新闻原始项（与后端字段保持一致）
 */
export interface ConferenceVideoItem {
    id: number;
    title: string;
    conferenceDate: string; // 发布时间/会议时间
    location?: string;
    registrationDeadline?: string;
    estimatedAttendees?: number;
    organizer?: string;
    contactPhone?: string;
    description?: string | null;
    news?: string | null;
    tags?: string | Tag[]; // 可能返回逗号分隔字符串
    coverImageUrl?: string;
    videoUrl?: string;
    videoThumbnailUrl?: string;
    videoDuration?: string | number | null; // 支持字符串 hh:mm:ss / mm:ss / 中文格式
    status?: boolean;
    isDel?: boolean;
    createTime?: string;
    updateTime?: string;
}

export interface ConferenceNewsItem {
    id: number;
    title: string;
    conferenceDate: string; // 发布时间/会议时间
    location?: string;
    registrationDeadline?: string;
    estimatedAttendees?: number;
    organizer?: string;
    contactPhone?: string;
    description?: string | null;
    news?: string | null;
    tags?: string | Tag[];
    coverImageUrl?: string;
    videoUrl?: string;
    videoThumbnailUrl?: string;
    videoDuration?: string | number | null;
    newsContent?: string; // 详情内容直接在列表返回
    status?: boolean;
    isDel?: boolean;
    createTime?: string;
    updateTime?: string;
}

/**
 * 通用分页响应（与后端 page/limit/total/totalPage 对齐）
 */
export interface PaginatedList<T> {
    page: number;
    limit: number;
    totalPage: number;
    total: number;
    list: T[];
}

export type VideoListResponse = PaginatedList<ConferenceVideoItem>;
export type NewsListResponse = PaginatedList<ConferenceNewsItem>;

/**
 * 会议相关类型
 */
export interface Conference {
    id: number;
    title: string;
    summary: string;
    eventTime: string;
    location: string;
    registrationDeadline: string;
    estimatedAttendees: number;
    organizer: string;
    contactPhone: string;
    isRegistered?: boolean; // 用户是否已报名
    createTime?: string;
    updateTime?: string;
}

export interface CreateConferenceRequest {
    title: string;
    summary: string;
    eventTime: string;
    location: string;
    registrationDeadline: string;
    estimatedAttendees: number;
    organizer: string;
    contactPhone: string;
}

export interface ConferenceListResponse {
    list: Conference[];
    total: number;
    pageNum: number;
    pageSize: number;
}

export interface ConferenceRegistrationRequest {
    participantUnitName: string;
    conferenceId: number;
    participantType: string;
    numberOfAttendees: number;
    userId: number;
    purposeOfAttending: string;
    contactPhone: string;
}

/**
 * 通用响应类型
 */
export interface ApiResponse<T = any> {
    code: number;
    message: string;
    data: T;
}

// =====================================================================================
// API 函数 (API Functions)
// =====================================================================================

/**
 * 标签管理 API
 */
export const tagApi = {
    /**
     * 创建标签
     */
    create: (data: CreateTagRequest) => {
        // @ts-ignore
        return $request.post({ url: 'front/industry-belt/tag/create', params: data })
    },

    /**
     * 获取标签列表
     */
    getList: () => {
        // @ts-ignore
        return $request.get({ url: 'front/industry-belt/tag/list' })
    }
};

/**
 * 内容管理 API
 */
export const contentApi = {
    /**
     * 创建内容
     */
    create: (data: CreateContentRequest) => {
        // @ts-ignore
        return $request.post({ url: 'front/industry-belt/content/create', params: data })
    },

    /**
     * 获取内容列表（兼容：会议视频 / 会议新闻）
     * - VIDEO: front/industry/conference/videoList
     * - NEWS: front/industry/conference/newsList
     */
    getList: (params?: ContentListRequest): Promise<VideoListResponse | NewsListResponse | { list: any[]; total: number; pageNum: number; pageSize: number }> => {
        const contentType = params?.contentType
        const page = (params as any)?.pageNum ?? (params as any)?.page ?? 1
        const limit = (params as any)?.pageSize ?? (params as any)?.limit ?? 20
        const reqParams = { page, limit }
        // @ts-ignore
        if (contentType === 'VIDEO') {
            return $request.get({ url: 'front/industry/conference/videoList', params: reqParams })
        }
        // @ts-ignore
        if (contentType === 'NEWS') {
            return $request.get({ url: 'front/industry/conference/newsList', params: reqParams })
        }
        // 其他类型暂不支持：返回空结构以避免页面报错
        return Promise.resolve({ list: [], total: 0, pageNum: page, pageSize: limit })
    },

    /**
     * 获取内容详情
     */
    getDetail: (id: number) => {
        // @ts-ignore
        return $request.get({ url: `front/industry-belt/content/detail/${id}` })
    },

    /**
     * 更新内容
     */
    update: (data: UpdateContentRequest) => {
        // @ts-ignore
        return $request.post({ url: 'front/industry-belt/content/update', params: data })
    },

    /**
     * 删除内容
     */
    delete: (id: number) => {
        // @ts-ignore
        return $request.delete({ url: `front/industry-belt/content/delete/${id}` })
    }
};

/**
 * 会议管理 API
 */
export const conferenceApi = {
    /**
     * 创建会议
     */
    create: (data: CreateConferenceRequest) => {
        // @ts-ignore
        return $request.post({ url: 'front/industry-belt/conference/create', params: data })
    },

    /**
     * 获取会议列表
     */
    getList: (params?: { pageNum?: number; pageSize?: number }) => {
        // @ts-ignore
        return $request.get({ url: 'front/industry/conference/futureList', params })
    },

    /**
     * 获取会议详情
     */
    getDetail: (id: number) => {
        // @ts-ignore
        return $request.get({ url: `front/industry-belt/conference/detail/${id}` })
    },

    /**
     * 用户报名会议
     */
    register: (data: ConferenceRegistrationRequest) => {
        // @ts-ignore
        return $request.post({ url: 'front/industry/conference/register', params: data })
    },

    /**
     * 删除会议
     */
    delete: (id: number) => {
        // @ts-ignore
        return $request.delete({ url: `front/industry-belt/conference/delete/${id}` })
    }
};

// =====================================================================================
// 统一导出 (Unified Exports)
// =====================================================================================

/**
 * 产业带模块 API 统一导出
 */
export const industryBeltApi = {
    tag: tagApi,
    content: contentApi,
    conference: conferenceApi
};

// 默认导出
export default industryBeltApi;
